package com.qire.manhua.model.entity;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.TeensModeManage;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.model.enumeration.HomeModuleType;

import java.util.ArrayList;

public class HomeModuleEntity {

    public final int id;
    public final String name;
    public final HomeModuleType homeModuleType;

    public HomeModuleEntity(int id, String name, int direction) {
        this(id, name, HomeModuleType.build(direction));
    }
    public HomeModuleEntity(int id, String name, HomeModuleType homeModuleType) {
        this.id = id;
        this.name = name;
        this.homeModuleType = homeModuleType;
    }

    public AutoVariable[] buildModuleViewModels() {
        return homeModuleType.buildModuleViewModels(this);
    }

    //region    #基础模块 begin

    private final ArrayList<HomeModuleItemEntity> moduleItemList = new ArrayList<>();
    private int pageNo = 1;

    public void addModuleItem(int bookId, String bookName, String title, String shortTitle, String coverImg, String[] tags, int freeNum, String floatTips,
                              int lastReadChapterId, String lastReadChapterName, boolean  isTeensVisible, boolean needShowSerial) {
        moduleItemList.add(new HomeModuleItemEntity(bookId, bookName, title, shortTitle, coverImg, tags,
                freeNum, floatTips, lastReadChapterId, lastReadChapterName, isTeensVisible, needShowSerial));
    }

    public void addModuleItem(int bookId, String bookName, String title, String shortTitle, String coverImg, String[] tags,
                              int freeNum, String floatTips, int lastReadChapterId, String lastReadChapterName, boolean  isTeensVisible) {
        moduleItemList.add(new HomeModuleItemEntity(bookId, bookName, title, shortTitle, coverImg, tags,
                freeNum, floatTips, lastReadChapterId, lastReadChapterName, isTeensVisible));
    }

    public void clearModuleItemList() {
        moduleItemList.clear();
    }

    public ArrayList<HomeModuleItemEntity> moduleItemList() {
        return moduleItemList;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int pageNo() {
        return pageNo;
    }

    public class HomeModuleItemEntity {

        public final HomeModuleEntity parent = HomeModuleEntity.this;

        public final int      bookId;
        public final String   bookName;
        public final String   title;
        public final String   shortTitle;
        public final String   coverImg;
        public final String[] tags;
        public final int      freeNum;
        public final String   floatTips;            // 空值将不会展示
        public final int      lastReadChapterId;
        public final String   lastReadChapterName;
        private final boolean isTeensVisible;       // 是青少年可见的
        public final boolean needShowSerial;       // 需要显示序号

        public HomeModuleItemEntity(int bookId, String bookName, String title, String shortTitle, String coverImg, String[] tags,
                                    int freeNum, String floatTips, int lastReadChapterId, String lastReadChapterName, boolean  isTeensVisible) {
            this(bookId, bookName, title, shortTitle, coverImg, tags, freeNum, floatTips, lastReadChapterId, lastReadChapterName, isTeensVisible, false);
        }
        public HomeModuleItemEntity(int bookId, String bookName, String title, String shortTitle, String coverImg, String[] tags,
                                    int freeNum, String floatTips, int lastReadChapterId, String lastReadChapterName, boolean  isTeensVisible, boolean needShowSerial) {
            this.bookId                 = bookId;
            this.bookName               = bookName;
            this.title                  = title;
            this.shortTitle             = shortTitle;
            this.coverImg               = coverImg;
            this.tags                   = tags;
            this.freeNum                = freeNum;
            this.floatTips              = floatTips;
            this.lastReadChapterId      = lastReadChapterId;
            this.lastReadChapterName    = lastReadChapterName;
            this.isTeensVisible         = isTeensVisible;
            this.needShowSerial         = needShowSerial;
        }

        public CharSequence getTag() {
            if(ObjectUtil.nonNull(tags)) {
                StringBuilder stringBuilder = new StringBuilder(tags.length);
                for (String str : tags) {
                    stringBuilder.append(str).append(" ");
                }
                return stringBuilder.toString();
            }
            return "";
        }

        public boolean isTeensVisible() {
            return TeensModeManage.checkVisibleIfNeed(isTeensVisible);
        }

        public ComicBookEntity toComicBookEntity() {
            return new ComicBookEntity(bookId,bookName, lastReadChapterId);
        }
    }

    //endregion #基础模块 end

    //region    #广告模块 begin

    private HomeModuleAdvertEntity advertEntity;

    public void setAdvertEntity(String adImgPath, boolean isShow, /*HomeModuleAdvertType advertType,*/ String comicId, String linkUrl, String linkTitle) {
        this.advertEntity = new HomeModuleAdvertEntity(adImgPath, isShow, comicId, linkUrl, linkTitle);
    }

    public HomeModuleAdvertEntity advertEntity() {
        return this.advertEntity;
    }

    public class HomeModuleAdvertEntity {

        public final HomeModuleEntity parent = HomeModuleEntity.this;
        public final String adImgPath;
        public final boolean isShow;
//        public final HomeModuleAdvertType advertType;

        public final int comicId;
        public final String linkUrl;
        public final String linkTitle;

        public HomeModuleAdvertEntity(String adImgPath, boolean isShow, /*HomeModuleAdvertType advertType,*/ String comicId, String linkUrl, String linkTitle) {
            this.adImgPath  = adImgPath;
            this.isShow     = isShow;
//            this.advertType = advertType;
            this.comicId    = Integer.valueOf(comicId);
            this.linkUrl    = linkUrl;
            this.linkTitle  = linkTitle;
        }

    }

    //endregion #广告模块 end

    //region    #排行榜模块 begin

    private final ArrayList<HomeModuleRankEntity> moduleRankList = new ArrayList<>();

    public ArrayList<HomeModuleRankEntity> moduleRankList() {
        return moduleRankList;
    }

    public void clearModuleRankList() {
        moduleRankList.clear();
    }

    public void addModuleRankItem(HomeModuleRankEntity rankEntity) {
        moduleRankList.add(rankEntity);
    }

    public class HomeModuleRankEntity {
        public final String rankTitle;
        public final ArrayList<HomeModuleRankItemEntity> rankInfoList = new ArrayList<>();

        public HomeModuleRankEntity(String rankTitle) {
            this.rankTitle = rankTitle;
        }

        public boolean isNotEmpty() {
            return !rankInfoList.isEmpty();
        }

        public void addRankItemEntity(int bookId, String coverUrl, String bookName, String popularity, String describe) {
            int    index        = rankInfoList.size()+1;
            rankInfoList.add(new HomeModuleRankItemEntity(bookId, coverUrl, bookName, popularity, describe, index));
        }

        public class HomeModuleRankItemEntity {

            public final int    bookId;
            public final String coverUrl;
            public final String bookName;
            public final String popularity;
            public final String describe;
            public final int    index;

            public HomeModuleRankItemEntity(int bookId, String coverUrl, String bookName, String popularity, String describe, int index) {
                this.bookId     = bookId;
                this.coverUrl   = coverUrl;
                this.bookName   = bookName;
                this.popularity = popularity;
                this.describe   = describe;
                this.index      = index;
            }

            public ComicBookEntity toComicBookEntity() {
                return new ComicBookEntity(bookId, bookName);
            }
        }

    }

    //endregion #排行榜模块 end

    public boolean existMore() {
        return id > 0;
    }

}
